#include <iostream>

using std::cout;
using std::endl;

class Complex
{
public:
    Complex(double dreal, double dimage)
    : _dreal(dreal)
    , _dimage(dimage)
    {   cout << "Complex(double,double)" << endl;   }

    void print() const
    {
        cout << _dreal << " + " << _dimage << " i" << endl;
    }

    //友元函数之普通函数
    //推荐使用
    friend Complex operator+(const Complex &, const Complex &);

    //左操作数(对象本身)发生了变化, 其返回值使用了引用形式
    Complex & operator+=(const Complex & rhs)
    {
        cout << "operator+=(const Complex &)" << endl;
        _dreal += rhs._dreal;
        _dimage += rhs._dimage;
        return *this;
    }

private:
    double _dreal;
    double _dimage;
};

//运算符重载之友元函数(普通函数)
Complex operator+(const Complex & lhs, const Complex & rhs)
{
    Complex tmp(lhs._dreal + rhs._dreal, lhs._dimage + rhs._dimage);
    return tmp;
}

int main()
{
    int a = 1, b = 2;
    int c = a + b;//内置类型的运算符不能重载
    cout << "c:" << c << endl;

    cout <<"&a:" << &a << endl;
    //a+=b的返回值是a本身，可以取地址, 是左值
    cout << "&(a += b): " << &(a+=b) <<endl;

    Complex c1(1, 2), c2(3, 4);
    cout << "c1:";
    c1.print();
    cout << "c2:";
    c2.print();

    //现在没有相应的运算符可以使用
    Complex c3 = c1 + c2;
    cout << "c3:";
    c3.print();

    &(c1 += c2);
    //c1.operator+=(c2);//完整展开形式

    return 0;
}

